﻿using System;
using System.Collections.Generic;

namespace Common.Module.WebQQ
{
    /// <summary>
    /// WebQQ操作类，可以模仿WebQQ
    /// </summary>
    public class WebQQ
    {
        private Net.HttpWebClient client = null;
        private string LoginAccount = "";
        public WebQQ()
        {
            client = new Net.HttpWebClient();
        }

        /// <summary>
        /// 处理请求的URL
        /// </summary>
        private string host = "http://tqq.tencent.com:8000";
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        public void Login(string username, string password)
        {
            string postValues = "VER=1.1&CMD=Login&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + username + "&PS=" + password.GetStringMD5() + "&M5=1&LC=9326B87B234E7235";


            LoginAccount = username;
            string str = client.PostDataToURL(host, postValues);
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="message"></param>
        public void Speak(string to, string message)
        {
            string postValues = "VER=1.1&CMD=CLTMSG&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&UN=" + to + "&MG=" + message;
            client.PostData(host, postValues);
        }
        /// <summary>
        /// 得到好友列表
        /// </summary>
        /// <returns></returns>
        public List<string> GetFriends()
        {
            string postValues = "VER=1.1&CMD=List&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&TN=160&UN=0";
            string getData = client.PostDataToURL(host, postValues);
            getData = getData.Substring(getData.IndexOf("&UN=") + 4);
            string[] s = getData.Split(new char[] { ',' });
            List<string> friends = new List<string>();
            for (int i = 0; i < s.Length; i++)
            {
                if (!string.IsNullOrEmpty(s[i]))
                {
                    friends.Add(s[i]);
                }
            }
            return friends;
        }
        /// <summary>
        /// 得到在线好友列表
        /// </summary>
        /// <returns></returns>
        public List<QQFriend> GetStatFriends()
        {
            string postValues = "VER=1.1&CMD=Query_Stat&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&TN=160&UN=0";
            string getData = client.PostDataToURL(host, postValues);
            string data = getData.Substring(getData.IndexOf("&FC=") + 4);
            string Images = data.Substring(0, data.IndexOf("&FN=") - 1);
            string data2 = data.Substring(data.IndexOf("&ST=") + 4);
            string stat = data2.Substring(0, data2.IndexOf("&UN=") - 1);
            string data3 = data2.Substring(data2.IndexOf("&UN=") + 4);
            string account = data3.Substring(0, data3.IndexOf("&NK=") - 1);
            string data4 = data3.Substring(data3.IndexOf("&NK=") + 4);
            string name = data4.Substring(0, data4.Length - 1);

            string[] imgs = Images.Split(new char[] { ',' });
            string[] stats = stat.Split(new char[] { ',' });
            string[] accounts = account.Split(new char[] { ',' });
            string[] names = name.Split(new char[] { ',' });


            List<QQFriend> friends = new List<QQFriend>();
            for (int i = 0; i < imgs.Length; i++)
            {
                QQFriend f = new QQFriend();
                f.Account = accounts[i];
                f.Img = ((Convert.ToInt32(imgs[i]) / 3) + 1) + ".bmp";
                f.Name = names[i];
                if (stats[i] == "10")
                {
                    f.Stat = "上线";
                }
                else if (stats[i] == "20")
                {
                    f.Stat = "离线";
                }
                else if (stats[i] == "30")
                {
                    f.Stat = "忙碌";
                }
                friends.Add(f);
            }
            return friends;
        }
        /// <summary>
        /// 添加好友并发送一天消息(如果好友需要验证则消息从验证消息中发出)
        /// </summary>
        /// <param name="account"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public bool AddFriend(string account, string message)
        {
            string postValues = "VER=1.1&CMD=AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&UN=" + account;
            string getData = client.PostDataToURL(host, postValues);
            getData = getData.Substring(getData.IndexOf("&CD=") + 4);
            getData = getData.Substring(0, 1);
            if (getData == "0")
            {
                Speak(account, message);
                return true;
            }
            else if (getData == "1")
            {
                CheckFriend(account, message);
                return true;
            }
            else
            {
                return false;
            }
            //    Common.LogManager.GetInstance().Log(getData);
        }
        /// <summary>
        /// 加为好友请求验证
        /// </summary>
        /// <param name="account"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        private void CheckFriend(string account, string message)
        {
            string postValues = "VER=1.1&CMD=Ack_AddToList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&UN=" + account + "&CD=2&RS=" + message;
            string getData = client.PostDataToURL(host, postValues);
        }
        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="account"></param>
        public void DeleteFriend(string account)
        {
            string postValues = "VER=1.1&CMD=DelFromList&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount + "&UN=" + account + "";
            string getData = client.PostDataToURL(host, postValues);
        }
        /// <summary>
        /// 获取消息
        /// </summary>
        /// <returns></returns>
        public List<QQMessage> GetMessage()
        {
            string postValues = "VER=1.1&CMD=GetMsgEx&SEQ=" + DateTime.Now.Ticks.ToString().Substring(7, 7) + "&UIN=" + LoginAccount;
            string getData = client.PostDataToURL(host, postValues);
            if (getData.IndexOf("&MT=&UN=") < 0)
            {
                string data = getData.Substring(getData.IndexOf("&MT=") + 4);
                string type = data.Substring(0, data.IndexOf("&UN=") - 1);
                string data2 = data.Substring(data.IndexOf("&UN=") + 4);
                string account = data2.Substring(0, data2.IndexOf("&MG=") - 1);
                string data4 = data2.Substring(data2.IndexOf("&MG=") + 4);
                string message = data4.Substring(0, data4.Length - 1);

                string[] MessageType = type.Split(new char[] { ',' });
                string[] accounts = account.Split(new char[] { ',' });
                string[] Messages = message.Split(new char[] { ',' });
                List<QQMessage> mess = new List<QQMessage>();
                for (int i = 0; i < MessageType.Length; i++)
                {
                    if (MessageType[i].ToString().Equals("9"))
                    {
                        QQMessage q = new QQMessage();
                        q.Account = accounts[i];
                        q.Message = Messages[i];
                        q.Type = MessageType[i];
                        mess.Add(q);
                    }
                }
                return mess;
            }
            else
            {
                return null;
            }
        }
    }
}
